home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / FlightSim / flight.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  5KB  |  321 lines

  1. #include "flight.h"
  2.  
  3.  
  4.  
  5. WindowPtr        theWind;    /*the main window*/
  6. WindowRecord    windRecord;    /*storage for the window*/
  7.  
  8.  
  9. GrafPtr            windManPort;    /*window manager port*/
  10. EventRecord     theEvent;    /*the main event*/
  11.  
  12.  
  13. MenuHandle        appleMenu, fileMenu, editMenu;
  14.  
  15.  
  16. main()
  17. {
  18.     Init();
  19.     MakeMenus();
  20.     MakeWindow();
  21.     AddrsSet();
  22.  
  23.     MakeGrid();
  24. DebugStr("\PHello");
  25.  
  26.     while (1)
  27.     {
  28.         SystemTask();
  29.         if (GetNextEvent(everyEvent, &theEvent))
  30.             DoEvent();
  31.     }
  32. }
  33.  
  34. DoEvent()
  35. {
  36.     switch (theEvent.what) {
  37.     case mouseDown:
  38.         DoMouseDown();
  39.         break;
  40.     case keyDown:
  41.     case autoKey:
  42.         DoKeyDown();
  43.         break;
  44.     case updateEvt:
  45.         DoUpdate();
  46.         break;
  47.     case diskEvt:
  48.         break;
  49.     case activateEvt:
  50.         DoActivate();
  51.         break;
  52.     }
  53. }
  54.  
  55. DoMouseDown()
  56. {
  57.     int thePart;
  58.     WindowPtr whichWindow;
  59.  
  60.     thePart = FindWindow( theEvent.where, &whichWindow);
  61.  
  62.     switch (thePart) {
  63.     case inDesk:
  64.         break;
  65.     case inMenuBar:
  66.         DoMenuClick();
  67.         break;
  68.     case inSysWindow:
  69.         SystemClick(&theEvent, whichWindow);
  70.         break;
  71.     case inContent:
  72.         DoContent(whichWindow);
  73.         break;
  74.     case inDrag:
  75.         DoDrag(whichWindow);
  76.         break;
  77.     case inGrow:
  78.         DoGrow(whichWindow);
  79.         break;
  80.     case inGoAway:
  81.         if (TrackGoAway(whichWindow, theEvent.where))
  82.             DoQuit();
  83.         break;
  84.     case inZoomIn:
  85.     case inZoomOut:
  86.         DoZoom(whichWindow, thePart);
  87.         break;
  88.     }
  89. }
  90.  
  91. DoMenuClick()
  92. {
  93.     long menuChoice;
  94.  
  95.     menuChoice = MenuSelect(theEvent.where);
  96.     DoMenuChoice(menuChoice);
  97. }
  98.  
  99. DoMenuChoice(menuChoice)
  100. long menuChoice;
  101. {
  102.     switch(HiWord(menuChoice)) {
  103.     case MENU_APPLE:
  104.         DoAppleChoice( LoWord(menuChoice) );
  105.         break;
  106.     case MENU_FILE:
  107.         DoFileMenu(LoWord(menuChoice));
  108.         break;
  109.     case MENU_EDIT:
  110.         DoEditMenu(LoWord(menuChoice));
  111.         break;
  112.     }
  113.  
  114.     HiliteMenu(0); /* unhilight the selected menu */
  115. }
  116.  
  117. DoAppleChoice(theItem)
  118. int theItem;
  119. {
  120.     char    accName[256];
  121.     int        accNum, i;
  122.     
  123.     if (theItem EQ 1)    
  124.         Alert(ALRT_ABOUT, NIL);
  125.     else
  126.     {
  127.         GetItem(appleMenu, theItem, accName);
  128.         accNum = OpenDeskAcc(accName);
  129.     }
  130. }
  131.  
  132. DoFileMenu(theItem)
  133. int theItem;
  134. {
  135.     if (theItem EQ 1)
  136.         DoQuit();
  137. }
  138.  
  139. DoQuit()
  140. {
  141.     ExitToShell();
  142. }
  143.  
  144. DoEditMenu(theItem)
  145. int theItem;
  146. {
  147.     switch(theItem) {
  148.     case 1: /* Undo */
  149.         SystemEdit(undoCmd);
  150.         break;
  151.     case 3: /* Cut */
  152.         SystemEdit(cutCmd);
  153.         break;
  154.     case 4: /* Copy */
  155.         SystemEdit(copyCmd);
  156.         break;
  157.     case 5: /* Paste */
  158.         SystemEdit(pasteCmd);
  159.         break;
  160.     case 6: /* Clear */
  161.         SystemEdit(clearCmd);
  162.         break;
  163.     }
  164. }
  165.  
  166. DoContent(whichWindow)
  167. WindowPtr whichWindow;
  168. {
  169.     Point    localPt;
  170.  
  171.     if (whichWindow NEQ FrontWindow())
  172.         SelectWindow(whichWindow);
  173.     else
  174.     {
  175.         localPt = theEvent.where;
  176.         GlobalToLocal(&localPt);
  177.         DoMouseMove(&localPt);
  178.     }
  179. }
  180.  
  181. DoDrag(whichWindow)
  182. WindowPtr whichWindow;
  183. {
  184.     Rect limitRect;
  185.  
  186.     if (whichWindow NEQ FrontWindow())
  187.         SelectWindow(whichWindow);
  188.     else
  189.     {
  190.         SetRect(&limitRect, 0, MENU_BAR_HEIGHT, 5000, 5000);
  191.         DragWindow(whichWindow, theEvent.where, &limitRect);
  192.     }
  193. }
  194.  
  195. DoGrow(whichWindow)
  196. WindowPtr whichWindow;
  197. {
  198.     Rect limitRect;
  199.     long    newSize;
  200.  
  201. return;
  202.     SetRect(&limitRect, 50, 50, 2000, 2000);
  203.     newSize = GrowWindow(whichWindow, theEvent.where, &limitRect);
  204.     if (newSize)
  205.     {
  206.         EraseRect(&whichWindow->portRect);
  207.         SizeWindow(whichWindow, LoWord(newSize), HiWord(newSize), TRUE);
  208.     }
  209. }
  210.  
  211. DoKeyDown()
  212. {
  213.     char    ch;
  214.     long    menuChoice;
  215.     
  216.     ch = (char)(theEvent.message & charCodeMask);
  217.     
  218.     if ((theEvent.modifiers & cmdKey)
  219.         AND (theEvent.what NEQ autoKey)) /* ignore repeats */
  220.     {
  221.         menuChoice = MenuKey(ch);
  222.         if (menuChoice)
  223.             DoMenuChoice(menuChoice);
  224.     }
  225. }
  226.  
  227. DoUpdate()
  228. {
  229.     GrafPtr savePort;        /* to save and restore the old port */
  230.     WindowPtr whichWindow;
  231.  
  232.     whichWindow = (WindowPtr) (theEvent.message);
  233.  
  234.     BeginUpdate(whichWindow);    /* reset ClipRgn etc to only redraw what's
  235.                                necessary. */
  236.  
  237.         GetPort(&savePort);        /* don't trash the port; we might be
  238.                                    updating an inactive window */
  239.         SetPort(whichWindow);    /* work in the specified window */
  240.  
  241.         if (whichWindow EQ theWind)
  242.         {
  243.             EraseRect(&(whichWindow->portRect));
  244.             DoForward();
  245.         }
  246.     EndUpdate(whichWindow);
  247.  
  248.     SetPort(savePort);
  249. }
  250.  
  251. DoActivate()
  252. {
  253.     WindowPtr whichWindow;
  254.  
  255.     whichWindow = (WindowPtr) (theEvent.message);
  256.     if (whichWindow EQ theWind)
  257.         /*DrawGrowIcon(whichWindow);*/
  258.  
  259.     if (theEvent.modifiers & activeFlag)
  260.         SetPort(whichWindow);
  261. }
  262.  
  263. DoZoom(whichWindow, thePart)
  264. WindowPtr    whichWindow;
  265. int            thePart;
  266. {
  267. return;
  268.  
  269.     if (TrackBox(whichWindow, theEvent.where, thePart))
  270.         ZoomWindow(whichWindow, thePart, TRUE);
  271. }
  272.  
  273.  
  274.  
  275. Init()
  276. {
  277.     pascal void restartProc();
  278.  
  279.     InitGraf(&thePort);
  280.     InitFonts();
  281.     InitWindows();
  282.     InitMenus();
  283.     TEInit();
  284.     InitDialogs(restartProc);        
  285.     InitCursor();
  286.     
  287.     FlushEvents(everyEvent, 0);
  288.  
  289.     GetWMgrPort(&windManPort);    /* get whole screen port that window mgr
  290.                    uses */
  291.     SetPort(windManPort);    /* and start off with it */
  292.  
  293.     SetEventMask(everyEvent);
  294. }
  295.  
  296. /* when the world comes to an end */
  297. pascal void
  298. restartProc()
  299. {
  300.     ExitToShell();
  301. }
  302.  
  303. MakeMenus()
  304. {
  305.     appleMenu = GetMenu(MENU_APPLE);
  306.     AddResMenu(appleMenu, 'DRVR');
  307.     InsertMenu(appleMenu, 0);
  308.     
  309.     fileMenu = GetMenu(MENU_FILE);
  310.     InsertMenu(fileMenu, 0);
  311.     
  312.     editMenu = GetMenu(MENU_EDIT);
  313.     InsertMenu(editMenu, 0);
  314.     
  315.     DrawMenuBar();
  316. }
  317.  
  318. MakeWindow()
  319. {
  320.     theWind = GetNewWindow(WIND_MAIN, &windRecord, (WindowPtr)-1L);    
  321. }